<template>
  <div class="my-comment">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id" :title="item.content">
        <template #title>
          <div class="cmt-cell">
            <div class="left-box">
              <van-image fit="cover" round :src="item.aut_photo" />

              <div class="info-box">
                <div class="name">{{ item.aut_name }}</div>
                <div class="content">{{ item.content }}</div>
                <div>
                  <span class="time">{{ $relvTime(item.pubdate) }}</span>
                  <van-button @click="clickReply(item)" plain size="mini"
                    >回复({{ item.reply_count }})</van-button
                  >
                </div>
              </div>
            </div>
            <van-icon name="like-o" />
          </div>
        </template>
      </van-cell>
    </van-list>

    <div class="field-box">
      <van-field
        @keyup.enter="send"
        v-model="cmt"
        center
        clearable
        placeholder="请输入评论内容"
      >
        <template #button>
          <van-button @click="send" size="small" type="info">发布</van-button>
        </template>
      </van-field>
    </div>

    <!-- 显示回复的sheet -->
    <van-action-sheet  v-model="replyShow" title="发表回复">
      <reply-cmt v-if="replyShow" :item="currentItem" @add="currentItem.reply_count++" />
    </van-action-sheet>
  </div>
</template>

<script>
// 导入接口
import { commontAPI, sendCmtAPI } from '@/api/commont'
import ReplyCmt from './ReplyCmt.vue'
export default {
  components: {
    ReplyCmt
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      cmt: '',
      offset: null,
      replyShow: false,
      currentItem: {}
    }
  },
  methods: {
    clickReply (item) {
      // 显示出回复面板
      this.replyShow = true
      // 点谁就拿到谁存到currentItem
      // currenItem就会自动流入到子
      this.currentItem = item
    },
    // 发送评论
    async send () {
      // 非空判断
      if (this.cmt === '') return this.$toast('请输入内容')
      // 不为空，要添加评论
      const res = await sendCmtAPI({
        // 发评论传文章id
        target: this.$route.params.id,
        content: this.cmt
      })
      // 把添加后的评论放到数组里即可
      this.list.unshift(res.data.new_obj)
      // 输入框清空
      this.cmt = ''
    },
    async onLoad () {
      const res = await commontAPI({
        // 因为现在获取的是评论，就要传a
        type: 'a',
        // 获取评论这里传文章id
        source: this.$route.params.id,
        offset: this.offset,
        // 一次查10条
        limit: 10
      })

      // 把本次返回的最后一条存到offset里
      // 作为下一次查询的开始
      this.offset = res.data.last_id
      this.list.push(...res.data.results)
      // 把本次加载状态改为false
      this.loading = false

      // 判断是否全部加载完成
      if (this.list.length >= res.data.total_count) {
        this.finished = true
      }
    }
  }
}
</script>

<style lang="less" scoped>
.my-comment {
  padding-bottom: 53px;

  ::v-deep {
    .field-box {
      position: fixed;
      left: 0;
      bottom: 0;
      width: 100%;
    }

    .cmt-cell {
      display: flex;
      justify-content: space-between;

      .left-box {
        display: flex;
        .van-image {
          width: 40px;
          height: 40px;
          margin-right: 10px;
        }
        .info-box {
          .name {
            color: #466b9d;
          }
          .content {
            margin: 10px 0;
          }
          .time {
            color: gray;
            margin-right: 10px;
          }
          .van-button {
            vertical-align: middle;
            font-size: 12px;
          }
        }
      }
    }
  }
}
</style>
